<template>
  <div class="scorelist zanwei-top">
    <head-nav :isBack="isBack"></head-nav>
    <div class="newlist">
      <tab
        :line-width="2"
        :active-color="primaryColor"
        custom-bar-width="21vw"
        v-model="index"
      >
        <tab-item
          class="vux-center"
          active-class="active6"
          :selected="activetext === item.JFHLFL"
          v-for="(item, index) in lanmu"
          @click="activetext = item"
          @on-item-click="handlerindex"
          :key="index"
        >{{ item.JFHLFL }}
        </tab-item
        >
      </tab>
      <swiper
        v-model="index"
        :height="maxheight"
        :show-dots="false"
        :threshold="120"
        @on-index-change="handlerindex"
      >
        <swiper-item
          v-for="(item, index) in newlanmulist"
          :key="index"
          :style="{ overflowY: 'scroll' }"
        >
          <ul
            v-infinite-scroll="loadMore"
            infinite-scroll-disabled="loading"
            infinite-scroll-distance="10"
            infinite-scroll-immediate-check="false"
          >
            <li v-for="(item, index) in item" :key="index">
              <div class="liinnwer" @click="goDetailFn(item.ID)">
                <div class=" image">
                  <div id="marktip">
                    <div id="marktipcontent" :style="backgroundColor">仅限会员</div>
                  </div>
                  <img :src="item.TP[0]" alt=""/>
                </div>
                <div class=" text">
                  <div class="textContent">
                    <h3 class="bt">{{ item.SPMC }}</h3>
                    <div class="sjbox">
                      <p class="rdl" :style="textColor">{{ item.JF }}积分</p>
                      <p class="sj" :style="textColor">数量剩余：{{ item.KC }}</p>
                    </div>
                  </div>
                  <i class="iconfont icon-jiantou"></i>
                </div>
              </div>
            </li>
          </ul>
          <div class="list-loading" v-if="loading">
            <i class="iconfont icon-loading"></i> 加载中...
          </div>
          <div v-else>
            <div v-if="forbid" class="forbid">
              没有更多了...
            </div>
          </div>
        </swiper-item>
      </swiper>
    </div>
  </div>
</template>

<script>
  import api from "@/api/api";
  import {Tab, TabItem, Swiper, SwiperItem} from "vux";
  import {Toast} from "mint-ui";

  export default {
    components: {
      Tab,
      TabItem,
      Swiper,
      SwiperItem
    },
    data() {
      return {
        index01: 0,
        activetext: "", //选中的文本
        index: 0,
        getBarWidth: function (index) {
          return (index + 1) * 22 + "px";
        },
        maxheight: "",
        //old
        isBack: false,
        loading: true,
        num: 1,
        numSize: 8,
        rows: [],
        forbid: false,
        lanmu: [],
        lanmulist: [],
        newlanmulist: [],
        copynewlanmulist: [],
        lanmuindex: 0,
        jfhlfl: "" // 1:换礼品|2:换爆款|3:换流量|4:功能券
      };
    },
    methods: {
      handlerindex(index) {
        let page = this;
        //获取当前切换的索引
        let lmIndex = {};
        lmIndex.index = index;
        lmIndex = JSON.stringify(lmIndex);
        sessionStorage.setItem("lmIndex", lmIndex);
        page.lanmuindex = index;
        (page.jfhlfl = page.lanmu[index].ID), (page.num = 1);
        page.forbid = false;
        // page.newlanmulist = page.copynewlanmulist
        page.getListDataFn(page.jfhlfl, page.numSize, page.num);
      },
      //old
      goDetailFn(id) {
        let page = this;
        var userinfo = JSON.parse(sessionStorage.getItem("userinfo"));
        if (userinfo && userinfo.vipcode) {
          if (!this.$util.checkValidity()) {
            return
          }
          let userId = userinfo.vipcode;
          // 检查过期
          api.scoreToGiftDetail(page, userId, id, cb);

          function cb(res) {
            if (res.code === 0) {
              // 存状态
              let idx = page.lanmuindex;
              let lmIndex = {};
              lmIndex.index = idx;
              lmIndex = JSON.stringify(lmIndex);
              sessionStorage.setItem("lmIndex", lmIndex);
              page.$router.push("/scoredetail/" + id);
            } else {
              Toast({
                message: res.msg,
                duration: 2000
              });
              setTimeout(function () {
                page.rows = [];
                // 刷新页面
                new Promise((resolve, reject) => {
                  api.getLiftType(page, cb);

                  function cb(res) {
                    if (res.code == 0) {
                      let data = JSON.parse(res.data);
                      console.log(data);
                      page.lanmu = data;
                      page.jfhlfl = data[0].ID;
                      resolve(0);
                    } else {
                      Toast({
                        message: res.msg,
                        duration: 3000
                      });
                    }
                  }
                }).then(result => {
                  page.getListDataFn(page.jfhlfl, page.numSize, page.num);
                });
              }, 1000);
            }
          }
        } else {
          Toast({
            message: "您还不是会员，快去注册吧！",
            duration: 2000
          });
          setTimeout(function () {
            //          page.$router.push('/register')
            page.$router.push({path: "/register", query: {type: "score"}});
          }, 2000);
        }
      },
      loadMore() {
        let page = this;
        this.num++;
        // var num = this.lanmulist[page.lanmuindex].num;
        if (this.forbid) {
          return false;
        }
        this.loading = true;
        this.getListDataFn(this.jfhlfl, this.numSize, this.num);
        // console.log(this.num)
        // this.loading = false;
      },
      getListDataFn(jfhlfl, row, num) {
        let page = this;
        api.scoreToGift(page, jfhlfl, row, num, cb);

        function cb(res) {
          if (res.code == 0) {
            let data = JSON.parse(res.data);
            // console.log(data,'weigang')
            // console.log(data.rows)
            // let forbid =false;
            var rows = [];
            if (data.rows.length !== 0) {
              rows = data.rows.map(y => {
                let TP = y.TP.map(x => {
                  return page.host.filehost + x;
                });
                y.TP = TP;
                return y;
              });
              page.rows = rows;
              // forbid=false
              // console.log(page.rows,'567897984563221');
            } else {
              page.forbid = true;
              page.rows = rows;
            }
            if (num == 1) {
              page.forbid = false;
              //第一次请求全部
              var newrows = {
                id: jfhlfl,
                rows: data.rows
              };
              page.lanmulist.push(newrows);
              //按导航顺序排序 筛选
              var newlanmulist = [];
              for (var i = 0; i < page.lanmu.length; i++) {
                for (var k = 0; k < page.lanmulist.length; k++) {
                  if (page.lanmu[i].ID == page.lanmulist[k].id) {
                    newlanmulist[i] = page.lanmulist[k].rows;
                  }
                }
              }
              page.newlanmulist = newlanmulist;
              console.log(page.newlanmulist, "使用的数据");
            } else {
              console.log("下拉刷新");
              console.log(
                page.newlanmulist[page.lanmuindex].concat(page.rows),
                "page.newlanmulist"
              );
              page.newlanmulist[page.lanmuindex] = page.newlanmulist[
                page.lanmuindex
                ].concat(page.rows);
            }
          } else {
            Toast({
              message: res.msg,
              duration: 3000
            });
          }
          setTimeout(function () {
            page.loading = false;
          }, 1000);
        }
      }
      // checkFn(i, id) {
      //   this.lanmuindex = i
      //   this.jfhlfl = id
      //   this.num = 1
      //   this.rows = []
      //   this.loading = true
      //   this.getListDataFn(this.jfhlfl, this.numSize, this.num);
      // },
    },
    mounted() {
      let page = this;
      this.maxheight = document.documentElement.clientHeight - 95 + "px";
      // 返回状态
      let selStatus = JSON.parse(sessionStorage.getItem("lmIndex"));
      if (selStatus) {
        if (selStatus.index !== undefined) {
          console.log(typeof selStatus.index, selStatus.index);
          page.lanmuindex = selStatus.index;
        }
      } else {
        let lmIndex = {};
        lmIndex.index = page.lanmuindex;
        lmIndex = JSON.stringify(lmIndex);
        sessionStorage.setItem("lmIndex", lmIndex);
      }
      new Promise((resolve, reject) => {
        api.getLiftType(page, cb);

        function cb(res) {
          if (res.code == 0) {
            let data = JSON.parse(res.data);
            console.log(data);
            page.lanmu = data;
            page.jfhlfl = data[page.lanmuindex].ID;
            page.activetext = data[0].JFHLFL;
            resolve(0);
          } else {
            Toast({
              message: res.msg,
              duration: 3000
            });
          }
        }
      }).then(result => {
        for (var i = 0; i < page.lanmu.length; i++) {
          page.getListDataFn(page.lanmu[i].ID, page.numSize, page.num);
        }
        // page.getListDataFn(page.jfhlfl, page.numSize, page.num);
      });
    }
  };
</script>

<style lang="less" scoped>
  // @import "~vux/src/styles/1px.less";
  // @import "~vux/src/styles/center.less";

  .box {
    padding: 15px;
  }

  .active6 {
    color: #f05dc4;
    border-color: #f05dc4;
  }

  // .tab-swiper {
  //   background-color: #fff;
  //   height: 100px;
  // }
  // .vux-swiper-item{
  //   ul{
  //     height: 400px;
  //     overflow:scroll;
  //   }
  // }
  // .vux-slider{}
  // .vux-slider > .vux-swiper{
  //   height: 300px !important;
  // }
  // .vux-swiper-item{
  //  height: 300px !important;
  // }
  //old
  @import "../../assets/css/mixins.less";
  .forbid {
    padding: 0.2rem;
    text-align: center;
  }

  .scorelist {
    .newlist {
      @tithei: 0.86rem;
      // padding-top: @tithei;
      .newlist-title {
        position: fixed;
        left: 0;
        top: 48px;
        height: @tithei;
        width: 100vw;
        /*width: 100%;*/
        background: #f6f6f6;
        z-index: 99;
        display: flex;
        /*overflow: initial;*/
        overflow-y: scroll;
        /*flex-wrap: nowrap;*/
        /*overflow-x: scroll;*/
        .borderBotttomOne(#d4d4d4);

        div {
          /*flex: 1;*/
          min-width: 25%;
          display: flex;
          justify-content: center;
          align-items: center;
          font-size: 0.28rem;
          font-weight: bold;

          &.active {
            color: #f05dc4;
            border-bottom: #f05dc4 solid 0.04rem;
          }
        }
      }

      ul {
        li {
          position: relative;
          list-style: none;

          .liinnwer {
            display: flex;
            padding: 0.2rem 0.2rem 0 0.2rem;

            .image {
              width: 1.7rem;
              height: 1.5rem;
              /* overflow: hidden; */
              border: #d4d4d4 solid 1px;
              position: relative;

              img {
                width: 100%;
                height: 100%;
              }
            }

            .text {
              &::before {
                content: "";
                position: absolute;
                z-index: 2;
                bottom: 0;
                left: 0px;
                width: 100%;
                height: 1px;
                border-bottom: 1px solid #d4d4d4;
                transform: scaleY(0.5);
                transform-origin: 0 100%;
              }

              position: relative;
              margin-left: 0.2rem;
              flex: 1;
              display: flex;

              .textContent {
                flex: 1;
                display: flex;
                flex-direction: column;
                justify-content: flex-start;
              }

              i {
                margin-left: 0.2rem;
                font-size: 0.24rem;
                position: absolute;
                right: 0.16rem;
                top: 0.3rem;
              }

              .bt {
                font-size: 0.28rem;
                font-weight: bold;
                color: #000000;
                text-align: justify;
              }

              .ms {
                font-size: 0.24rem;
                color: #999999;
              }

              .sjbox {
                display: flex;
                justify-content: space-between;
                padding-top: 0.4rem;
                color: #999999;
                font-size: 0.28rem;

                .sj {
                  color: #f05dc4;
                  margin-right: 0.3rem;
                }

                .rdl {
                  color: #50cbf5;
                }
              }
            }
          }
        }
      }
    }
  }

  #marktip {
    width: 102%;
    height: 102%;
    background: transparent;
    overflow: hidden;
    position: absolute;
    left: -2%;
    top: -2%;
  }

  #marktipcontent {
    width: 1.2rem;
    height: 0.28rem;
    line-height: 0.28rem;
    background: #f05dc4;
    transform: rotate(-45deg) translate(-28%, -35%);
    font-size: 0.16rem;
    color: #ffffff;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .scrollable {
    background-color: #04be02 !important;
  }

  .vux-slider > .vux-swiper > .vux-swiper-item {
    overflow-y: scroll;
    touch-action: pan-y;
    -webkit-overflow-scrolling: touch;
  }
</style>
